package com.docMan.service.impl;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.stereotype.Service;

import com.docMan.bean.FtpFile;
import com.docMan.dao.pojo.Tchuman;
import com.docMan.service.FtpService;
import com.docMan.utils.FtpUtils;

@Service("ftpService")
public class FtpServiceImpl extends BaseServiceImpl<Tchuman> implements FtpService {

	@Override
	public String getFtpFile(String url, int port, String username,
			String password, String path, String fileName) {
		List<FtpFile> ftpFiles=new ArrayList<FtpFile>();
		FTPClient ftpClient=new FTPClient();
		try {
			ftpClient=FtpUtils.loginFtp("127.0.0.1", 211, "admin", "gqb101112");
			FTPFile[] filessFiles=ftpClient.listDirectories();
			boolean pathResult=ftpClient.changeWorkingDirectory(path);
			if(!pathResult){
				putFalse();putMessage("访问的文件夹不存在");
				return getMap();
			}
			FTPFile[] files=ftpClient.listFiles();
			for (FTPFile ftpFile : files) {
				String type="file";
				if(ftpFile.getType()!=FTPFile.FILE_TYPE){
					type="directory";
				}
				ftpFiles.add(new FtpFile(ftpFile.getName(),path,type));
			}
			put("files", ftpFiles);
		} catch (SocketException e) {
			
			e.printStackTrace();
			putFalse();putMessage("ftp登入失败");
		} catch (IOException e) {
			putFalse();putMessage("ftp文件不存在");
			e.printStackTrace();
		}finally{
			if(ftpClient.isConnected()){
				try {
					ftpClient.disconnect();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		putSuccess();
		return getMap();
	}

}
